function Js_GeneralItem(options){
	//Mang du lieu 
	this.defaultValue = options.defaultValue;
	this.arrListValue = new Array();
	this.columns = options.columns;
	this.block = options.block;
	//ID doi tuong
	this.input_id = options.input_id;
	//ID doi tuong
	this.input_select = options.input_select;	
	//Thong bao khi rong
	this._EMPLY_ALERT = 'Bạn chưa nhập thông tin đối tượng!';
	//Thong bao xac nhan xoa doi tuong
	this._CONFIRM_DELETE = 'Bạn chắc chắn muốn xóa đối tượng!';
	//Tao bang du lieu co san
	this.initialize();
}
Js_GeneralItem.prototype.convertdata = function(data){
	var self = this,
		arrTemp = {},
		arrResult = [],
		columns = this.columns;
	$.each(data, function(i, item) {
			arrTemp = {}
			for (var i = 0; i < columns.length; i++) {
				arrTemp[columns[i]] = (typeof(item[columns[i]]) !='undefined' ? item[columns[i]] : '');
			};
			arrResult.push(arrTemp);
		})
	return arrResult;
}
Js_GeneralItem.prototype.initialize = function(){
	var self = this;
	if(self.defaultValue){
		var arrDefault = $.parseJSON(self.defaultValue);
		arrDefault = self.convertdata(arrDefault);
		$.each(arrDefault, function(i, item) {
			self._add_item(item);
		})
		$(self.block).find('#'+this.input_id).val(self.defaultValue);
	}
}
$.extend(Js_GeneralItem.prototype, {
    _eTable: function(){
        var self = this;
        $(self.block).find(".eedit").unbind('dblclick').dblclick(function(){
            self.select(this);
        });
        $(self.block).find(".edelete").unbind('click').click(function(){
            self.delete(this);
        });
    },
})
//	Them mot van ban kem theo
Js_GeneralItem.prototype._add_item = function(arrValue){
	var shtml = '<tr class="row_data">';
	$.each(arrValue, function(i, item) {
		item = trim(item);
		shtml += '<td class="normal_label eedit" onmouseout="this.style.backgroundColor=\'\'" onmouseover="this.style.backgroundColor=\'#ABD8FF\'">' + item + '</td>';
	})
	shtml += '<td class="normal_label edelete"><button type="button" class="btn btn-danger btn-delete-decorators">DEL</button></td>';
	shtml += '</tr>';
	$('table#table_'+this.input_id+' tbody tr:last').before(shtml);
	this.arrListValue.push(arrValue);
	this._eTable();
}
Js_GeneralItem.prototype.alert_to = function(msg,type){
	if(type=='alert'){
		alert(msg); return false;
	}
	if(type=='confirm'){
		if(confirm(msg)){
			return true;
		}
		return false;
	}
	return false;
}
//	Them mot van ban kem theo
Js_GeneralItem.prototype.add_item = function(){
	var columns = this.columns,
		arrTemp = {};
	var arrValue = $(this.block).find(this.input_select).map(function(idx, elem){
		var value = $(elem).val();
		$(elem).val('');
		return value;
	}).get();
	var shtml = '<tr class="row_data">';
	var checkadd = 0;
	$.each(arrValue, function(i, item) {
		item = trim(item);
		if(item!='') checkadd = 1;
		shtml += '<td class="normal_label eedit" onmouseout="this.style.backgroundColor=\'\'" onmouseover="this.style.backgroundColor=\'#ABD8FF\'">' + item + '</td>';
	})
	if(checkadd){
		shtml += '<td class="normal_label edelete"><button type="button" class="btn btn-danger btn-delete-decorators">DEL</button></td>';
		shtml += '</tr>';
		$('table#table_'+this.input_id+' tbody tr:last').before(shtml);
		for (var i = 0; i < arrValue.length; i++) {
			arrTemp[columns[i]] = arrValue[i];
		};
		this.arrListValue.push(arrTemp);
		this.setInputValue();
		this._eTable();
	}else{
		this.alert_to(this._EMPLY_ALERT,'alert');
	}
	$(this.block).find(this.input_select)[0].focus();
}
Js_GeneralItem.prototype.select = function(td){
	var objJs = this;
	var text = $(td).text();
	$(td).text('');
	$('<textarea style="width:98%;min-width: 0;" rows="1" />').appendTo($(td)).val(text).select().blur(
		function(){
			var newText = $(this).val();
			$(this).parent().text(newText).find('textarea').remove();
			objJs.getInputValue();
			objJs.setInputValue();			
		});
}
Js_GeneralItem.prototype.delete = function(td){
	if(this.alert_to(this._CONFIRM_DELETE,'confirm')){
		$(td).parent().remove();
		this.getInputValue();
		this.setInputValue();
	}
}
Js_GeneralItem.prototype.setInputValue = function(){
	var strValue = JSON.stringify(this.arrListValue);
	$(this.block).find('#'+this.input_id).val(strValue);
}
Js_GeneralItem.prototype.getInputValue = function(){
	var arrValue = new Array(),
		arrListValue = new Array(),
		arrTemp = {},
		columns = this.columns;
	$('table#table_'+this.input_id+' tbody tr.row_data').each(function(){
		arrValue = $(this).children('td').map(function(){ return $(this).text();}).get();
		for (var i = 0; i < arrValue.length; i++) {
			arrTemp[columns[i]] = arrValue[i];
		};
		arrListValue.push(arrTemp);
    })
	this.arrListValue = arrListValue;
}
function trim(TRIM_VALUE){
	TRIM_VALUE = TRIM_VALUE.toString();
    if(TRIM_VALUE.length < 1){
        return"";
    }
    TRIM_VALUE = rtrim(TRIM_VALUE);
    TRIM_VALUE = ltrim(TRIM_VALUE);
    if(TRIM_VALUE==""){
        return "";
    }
    else{
        return TRIM_VALUE;
    }
} //End Function

function ltrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0){
        return"";
    }
    var iTemp = v_length -1;

    while(iTemp > -1){
        if(VALUE.charAt(iTemp) == w_space){
        }
        else{
            strTemp = VALUE.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;

    } //End While
    return strTemp;

} //End Function

function rtrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";
    var iTemp = 0;
    while(iTemp < v_length){
        if(VALUE.charAt(iTemp) == w_space){
        }
        else{
            strTemp = VALUE.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
} //End Function